package com.wj.ballcat.demo.model.entity;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.tangzc.mpe.actable.annotation.ColumnComment;
import com.tangzc.mpe.actable.annotation.Table;
import com.tangzc.mpe.annotation.Serializable;
import com.tangzc.mpe.bind.metadata.annotation.*;
import lombok.Data;

import java.util.List;

@Data
@Table(comment = "用户信息")
public class DemoUser {
	@ColumnComment("主键")
	private String id;

	@ColumnComment("用户名")
	private String username;

	@ColumnComment("头像")
	private String icon;

	@ColumnComment("密码")
	private String password;

	// 通过中间表关联所有相关的角色。
	// 通过中间表的形式需要使用@Bind*ByMid
	@BindEntityByMid(conditions = @MidCondition(
			midEntity = DemoUserRole.class, selfMidField = "userId", joinMidField = "roleId"
	), orderBy = @JoinOrderBy(field = "name"))
	@TableField(exist = false)
//	@Serializable + @TableName(autoResultMap = true) 两个注解，其中@Serializable是对@TableField的包装，指定了typeHandler为CustomJacksonTypeHandler
	@Serializable
	private List<DemoRole> roles;

//	// 关联该用户发布的所有文章（）
//	@BindEntity(conditions = @JoinCondition(selfField = "id", joinField = "publishedUserId"))
//	private List<Article> articles;



//	// 关联该用户发布的所有文章（"audit = 1" 表示的是Article下的audit为1的情况，customCondition的值只能是被关联表下的字段值，且会以and的形式添加在查询条件末尾。）
//	@BindEntity(conditions = @JoinCondition(selfField = "id", joinField = "publishedUserId"), customCondition = "audit = 1", orderBy = @JoinOrderBy(field = "publishedTime", isAsc = false))
//	private List<Article> articles;

	// 关联该用户发布的所有文章（"audit = 1" 表示的是Article下的audit为1的情况，customCondition的值只能是被关联表下的字段值，且会以and的形式添加在查询条件末尾。）
	@BindEntity(conditions = @JoinCondition(selfField = "id", joinField = "publishedUserId"), customCondition = "audit = 1", orderBy = @JoinOrderBy(field = "publishedTime", isAsc = false), last = "limit 3")
	@TableField(exist = false)
	private List<Article> articles;

	// 关联身份证完整信息
	// 其中‘conditions’的赋值，表示当前对象(User)的id属性与关联对象(IdentityCard)的userId属性相同
	// 即，如果当前User对象的id为1的话，那么IdentityCard数据中某条userId为1的数据会被关联过来
	@BindEntity(conditions = @JoinCondition(selfField = "id", joinField = "userId"))
	@TableField(exist = false)
	private IdentityCard idCard;


	// 同理只关联身份证号码，@BindField比@BindEntity多了entity、field字段。
	// 因为无法从属性的类型上直接获取实体，需要指明哪个实体的哪个字段。
	@BindField(entity = IdentityCard.class,
			field = "identityNumber",
			conditions = @JoinCondition(selfField = "id", joinField = "userId"))
	@ColumnComment("身份证号码")
	private String idCardNumber;
}
